package com.webapp.mail;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendMail
{
  String SMTPHost = "";
  String user = "";
  String password = "";

  String from = "";
  String to = "";
  String subject = "";
  String content = "";

  List<String> fileList = new ArrayList();

  public String getContent()
  {
    return this.content;
  }

  public void setContent(String content) {
    try {
      content = new String(content.getBytes("gb2312"), "gb2312");
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    this.content = content;
  }

  public String getFrom()
  {
    return this.from; }

  public void setFrom(String from) {
    this.from = from;
  }

  public String getPassword() {
    return this.password; }

  public void setPassword(String password) {
    this.password = password;
  }

  public String getSMTPHost() {
    return this.SMTPHost; }

  public void setSMTPHost(String host) {
    this.SMTPHost = host;
  }

  public String getSubject() {
    return this.subject;
  }

  public void setSubject(String subject) {
    try {
      subject = new String(subject.getBytes("gb2312"), "gb2312");
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    this.subject = subject;
  }

  public String getTo() {
    return this.to; }

  public void setTo(String to) {
    this.to = to;
  }

  public String getUser() {
    return this.user; }

  public void setUser(String user) {
    this.user = user;
  }

  public List<String> getFileList() {
    return this.fileList;
  }

  public void setFileList(List<String> fileList) {
    this.fileList = fileList;
  }

  public boolean send()
  {
    Properties props = new Properties();

    props.put("mail.smtp.host", this.SMTPHost);

    props.put("mail.smtp.auth", "true");
    try
    {
      SmtpAuth auth = new SmtpAuth();
      auth.setAccount(this.user, this.password);

      Session mailSession = Session.getDefaultInstance(props, auth);
      mailSession.setDebug(true);	//false:do not print log, true:print log
      
      MimeMessage message = new MimeMessage(mailSession);

      message.setFrom(new InternetAddress(this.from));

      message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(this.to));

      message.setSubject(this.subject);

      message.setSentDate(new Date());

      message.setHeader("X-Priority", "1");
      message.saveChanges();

      Multipart container = new MimeMultipart();

      BodyPart textBodyPart = new MimeBodyPart();

      textBodyPart.setContent(this.content, "text/html;charset=gb2312");

      container.addBodyPart(textBodyPart);

      String filename = "";
      for (int i = 0; (this.fileList != null) && (i < this.fileList.size()); ++i) {
        filename = (String)this.fileList.get(i);
        File attachfile = new File(filename);

        if (!(attachfile.exists()))
          continue;
        BodyPart fileBodyPart = new MimeBodyPart();

        FileDataSource fds = new FileDataSource(filename);
        fileBodyPart.setDataHandler(new DataHandler(fds));

        String attachName = fds.getName();
        attachName = new String(attachName.getBytes("gb2312"), 
          "gb2312");

        fileBodyPart.setFileName(attachName);

        container.addBodyPart(fileBodyPart);
      }

      message.setContent(container);

      Transport transport = mailSession.getTransport("smtp");

      transport.connect(this.SMTPHost, this.user, this.password);

      Transport.send(message, message.getAllRecipients());
      transport.close();
      return true;
    } catch (Exception ex) {
      ex.printStackTrace(); }
    return false;
  }

  public static void main(String[] args)
  {
    SendMail mail = new SendMail();

    String mycontent = "uksmtpapp.gsk.com66666666666666666666";
    List files = new ArrayList();

    Calendar cal = Calendar.getInstance();

    mail.setSMTPHost("uksmtpapp.gsk.com");
    mail.setUser("");
    mail.setPassword("");
    mail.setSubject("gsk report");
    mail.setFrom("kl811796@gsk.com");
    mail.setTo("kl811796@gsk.com,tripp.gu@inforim.cn");
    mail.setContent(mycontent);

    mail.setFileList(files);

    boolean status = mail.send();
    System.out.println(status);

    Calendar cal1 = Calendar.getInstance();

    System.out.println("发送邮件耗时(单位:毫秒)  " + (cal1.getTimeInMillis() - cal.getTimeInMillis()));
  }

  static class SmtpAuth extends Authenticator
  {
    String user;
    String password;

    void setAccount(String user, String password)
    {
      this.user = user;
      this.password = password;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
      return new PasswordAuthentication(this.user, this.password);
    }
  }
}